package com.jspx.txweb.action;

import com.jspx.boot.environment.Environment;
import com.jspx.boot.sign.AuditingType;
import com.jspx.boot.sign.LanguageRes;
import com.jspx.boot.sign.UserType;
import com.jspx.txweb.IRole;
import com.jspx.txweb.annotation.HttpMethod;
import com.jspx.txweb.annotation.Operate;
import com.jspx.txweb.annotation.Safety;
import com.jspx.txweb.IUserSession;
import com.jspx.txweb.table.ConnectApp;
import com.jspx.txweb.view.ConnectAppListView;
import com.jspx.utils.ArrayUtil;
import com.jspx.utils.BeanUtil;
import java.util.Date;

/**
 * Created by jspx.net maker
 * User: chenYuan
 * DateTime: 2018-11-20 11:48
 * jspx.user.action.ConnectApp
 * 数据管理
 */
@HttpMethod(caption = "应用授权管理")
public class ConnectAppAction extends ConnectAppListView {
    public ConnectAppAction() {

    }

    private Long[] idArray = null;
    @Safety(request = false)
    public void setIdArray(Long[] idArray) {
        this.idArray = idArray;
    }

    /**
     * 删除ConnectApp对象,支持数组方式
     * @throws Exception
     */
    @Operate(caption="删除")
    public void delete() throws Exception {
        setActionResult(ROC);
        //登陆判断
        if (isGuest()) {
            addFieldInfo(Environment.warningInfo, language.getLang(LanguageRes.notLogin));
            return;
        }
        IUserSession userSession = (IUserSession) getUserSession();

        Long[] ids = getLongArray("id", idArray);
        if (ArrayUtil.isEmpty(ids)&&getId()>0) ids = new Long[]{getId()};
        if (ArrayUtil.isEmpty(ids)) {
            addFieldInfo(Environment.warningInfo, language.getLang(LanguageRes.needSelect));
            return;
        }

        IRole role = getRole();
        //删除日志记录
        StringBuilder actionLog = new StringBuilder();
        for (long id : ids) {
            ConnectApp connectApp = (ConnectApp) connectAppDAO.get(ConnectApp.class, id);

            if (userSession.getUid()==connectApp.getPutUid()|| role.getUserType()==UserType.ADMINISTRATOR)
            {
                if (connectAppDAO.delete(ConnectApp.class, id, false) > 0) {
                    addActionMessage(language.getLang(LanguageRes.deleteSuccess) + connectApp.getId());
                    actionLog.append(BeanUtil.toString( connectApp));
                }
            }
        }
        setActionLogContent(actionLog.toString());
    }

     /**
     * 保存ConnectApp对象
     * @throws Exception
     */
    @Operate(caption="保存")
    public void save() throws Exception {
        //登陆判断
        if (isGuest()) {
            addFieldInfo(Environment.warningInfo, language.getLang(LanguageRes.notLogin));
            return;
        }
        IUserSession userSession = (IUserSession) getUserSession();
        ConnectApp connectApp = (ConnectApp) getBean(ConnectApp.class);
        //放入用户信息
        connectApp.setPutName(userSession.getName());
        connectApp.setPutUid(userSession.getUid());
        connectApp.setIp(request.getRemoteAddr());
        int x = connectAppDAO.save(connectApp);
        if (x > 0) {
            setActionLogContent(BeanUtil.toString( connectApp));
            addActionMessage(language.getLang(LanguageRes.saveSuccess) + x);
        } else {
            addFieldInfo(Environment.warningInfo, language.getLang(LanguageRes.namespaceError)+ x);
        }
    }

    /**
     * 编辑ConnectApp对象
     * @throws Exception
     */
    @Operate(caption="编辑")
    public void edit() throws Exception {

        IUserSession userSession = (IUserSession) getUserSession();
        ConnectApp connectApp = (ConnectApp) getBean(ConnectApp.class);
        ConnectApp connectAppOld = (ConnectApp) connectAppDAO.get(ConnectApp.class, connectApp.getId());
        if (connectAppOld == null) {
            addFieldInfo(Environment.warningInfo,language.getLang(LanguageRes.updateFailure));
            return;
        }
        if (userSession.getUid()!=connectAppOld.getPutUid())
        {
            //只有自己才能修改
            addFieldInfo(Environment.warningInfo, language.getLang(LanguageRes.needManagePower));
            return;
        }
        //编辑后需要重新审核
        connectApp.setAuditingType(AuditingType.NO);
        connectApp.setCreateDate(connectAppOld.getCreateDate());
        connectApp.setPutName(connectAppOld.getPutName());
        connectApp.setPutUid(connectAppOld.getPutUid());
        connectApp.setIp(request.getRemoteAddr());

        int x = connectAppDAO.update(connectApp);
        if (x > 0) {
            addActionMessage(language.getLang(LanguageRes.updateSuccess));
            setActionLogContent(BeanUtil.toString( connectApp));
            setActionResult("list");
        } else {
            addFieldInfo(Environment.warningInfo,language.getLang(LanguageRes.updateFailure)+ x);
        }
    }


    @Operate(caption = "提前")
    public void sortDate() throws Exception {
        setActionResult(ROC);
        if (isGuest()) {
            addFieldInfo(Environment.warningInfo, language.getLang(LanguageRes.notLogin));
            return;
        }

        Long[] ids = getLongArray("id", idArray);
        if (ArrayUtil.isEmpty(ids)) {
            addFieldInfo(Environment.warningInfo, language.getLang(LanguageRes.invalidParameter));
            return;
        }

        StringBuilder actionLog = new StringBuilder();
        for (Long id : ids) {
            ConnectApp connectApp = (ConnectApp)connectAppDAO.get(ConnectApp.class, id, false);
            if (connectApp == null) continue;
            connectApp.setSortDate(new Date());
            int r = connectAppDAO.update(connectApp, new String[]{"sortDate"});
            if (r > 0) {
              actionLog.append(id);
                addActionMessage(language.getLang(LanguageRes.saveSuccess));
            } else {
                addFieldInfo(Environment.warningInfo,language.getLang(LanguageRes.updateFailure));
            }
        }
        setActionLogTitle(ArrayUtil.toString(ids, "|"));
        setActionLogContent(actionLog);
    }


    private int sortType = 0;
    public void setSortType(int sortType) {
        this.sortType = sortType;
    }
    /**
    * 排序类型
    */
    @Operate(caption = "排序")
    public void sortType() throws Exception {
        setActionResult(ROC);
        Long[] ids = getLongArray("id", idArray);
        if (ArrayUtil.isEmpty(ids)) {
            addFieldInfo(Environment.warningInfo, language.getLang(LanguageRes.invalidParameter));
            return;
        }
        IUserSession userSession = (IUserSession) getUserSession();
        if (connectAppDAO.updateSortType(ids, getInt("sortType",sortType))) {
            addActionMessage(language.getLang(LanguageRes.saveSuccess));
            setActionLogContent(ArrayUtil.toString(ids, ","));
        } else {
            addFieldInfo(Environment.warningInfo,language.getLang(LanguageRes.updateFailure));
        }
    }

    /**
    * 置顶还原
    * @throws Exception
    */
    @Operate(caption = "置顶还原")
    public void clearSortType() throws Exception {
        setActionResult(ROC);
        Long[] ids = getLongArray("id", idArray);
        if (ArrayUtil.isEmpty(ids)) {
            addFieldInfo(Environment.warningInfo, language.getLang(LanguageRes.invalidParameter));
            return;
        }
        if (connectAppDAO.updateSortType(ids, 0)) {
            addActionMessage(language.getLang(LanguageRes.saveSuccess));
            setActionLogContent(ArrayUtil.toString(ids, ","));
        } else {
            addFieldInfo(Environment.warningInfo,language.getLang(LanguageRes.updateFailure));
        }
    }

    /**
     * ConnectApp对象,审核通过
     */
    @Operate(caption="审核通过")
    public void okAuditing()  {
        setActionResult(ROC);
        IRole role = getRole();
        if (role==null||role.getUserType()<UserType.INTENDANT)
        {
            addFieldInfo(Environment.warningInfo, language.getLang(LanguageRes.needConfigManagerPower));
            return;
        }
        Long[] ids = getLongArray("id",idArray);
        if (ArrayUtil.isEmpty(ids)) {
            addFieldInfo(Environment.warningInfo, language.getLang(LanguageRes.invalidParameter));
            return;
        }
        if (connectAppDAO.updateAuditing(ids, AuditingType.OK)) {
            addActionMessage("审核未通过成功");
            setActionLogContent(ArrayUtil.toString(ids, ","));
        } else
            addFieldInfo(Environment.warningInfo, language.getLang(LanguageRes.operationFailure));
    }

    /**
     * ConnectApp对象,审核通过
     */
    @Operate(caption = "审核不过")
    public void noAuditing() {
        setActionResult(ROC);
        IRole role = getRole();
        if (role==null||role.getUserType()<UserType.INTENDANT)
        {
            addFieldInfo(Environment.warningInfo, language.getLang(LanguageRes.needConfigManagerPower));
            return;
        }
        Long[] ids = getLongArray("id",idArray);
        if (ArrayUtil.isEmpty(ids)) {
            addFieldInfo(Environment.warningInfo, language.getLang(LanguageRes.invalidParameter));
            return;
        }
        if (connectAppDAO.updateAuditing(ids, AuditingType.NO)) {
            addActionMessage("设置审核未通过成功");
            setActionLogContent(ArrayUtil.toString(ids, ","));
        } else
            addFieldInfo(Environment.warningInfo, language.getLang(LanguageRes.operationFailure));
    }

    @Override
    public String execute() throws Exception {
        if (isMethodInvoked()) {
            clearData();
            connectAppDAO.evict(ConnectApp.class);
        }
        return super.execute();
    }
}